package org.zk.dubbo_provider.service.impl;


import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.zk.dubbo_provider.dao.CatalogMapper;
import org.zk.entity.Catalog;
import org.zk.entity.Lession;
import org.zk.service.CatalogService;
import com.alibaba.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.util.Arrays.asList;

/**
 * <p>
 * 目录表 服务实现类
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@Service(timeout = 10000)
@CacheConfig(cacheNames = "catalog")
public class CatalogServiceImpl implements CatalogService {

    @Resource
    private CatalogMapper catalogMapper;

    /*添加目录*/
    @Override
    public int addCatalog(Catalog catalog) {
        return catalogMapper.addCatalog(catalog);
    }

    /*查看目录*/
    @Cacheable(key = "'getCatalogByCid:'+#courseId")
    @Override
    public List<Lession> getCatalogByCid(int courseId) {
        Catalog catalog = catalogMapper.getCatalogByCid(courseId);
        String ca=catalog.getCatalogLessionLise();
        List<String> list=Arrays.asList(ca.split("_"));
        List<Lession> ls=new ArrayList<>();
        //分割出来的字符串封装成对象
        for (String str:list) {
            Lession lession=new Lession();
            lession.setLessionId(Integer.parseInt(str.substring(0,str.indexOf("."))));
            lession.setLessionTitle(str.substring(str.indexOf(".")));
            ls.add(lession);
        }
        return ls;
    }

    //获得string类型的目录
    @Override
    public String getStringCatalog(int courseId) {
        return catalogMapper.getCatalogByCid(courseId).getCatalogLessionLise();
    }

    //更新目录
    @Override
    public int updateCatalog(Catalog catalog) {
        return catalogMapper.updateCatalog(catalog);
    }

    public CatalogMapper getCatalogMapper() {
        return catalogMapper;
    }

    public void setCatalogMapper(CatalogMapper catalogMapper) {
        this.catalogMapper = catalogMapper;
    }

}
